package com.xinshujia.wxxcx.service.impl;

import com.xinshujia.wxxcx.mapper.AccelerationMapper;
import com.xinshujia.wxxcx.mapper.MachineMapper;
import com.xinshujia.wxxcx.pojos.info.MachineInfo;
import com.xinshujia.wxxcx.service.MachineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Cui Han
 * @Date 2024/5/17 13:45
 * @Description: TODO
 * @Version 1.0
 */
@Service
public class MachineServiceImpl implements MachineService {

    @Autowired
    private MachineMapper machineMapper;

    @Autowired
    private AccelerationMapper accelerationMapper;
    /**
     * 根据产线id查询所有机床
     *
     * @param productionLineId
     * @return
     */
    @Override
    public List<MachineInfo> findByProductionLineIdMachine(Long productionLineId) {
        List<MachineInfo> machineInfos = machineMapper.findByProductionLineIdMachine(productionLineId);
        // 这条产线上的机床是否在线（判断依据 - 当前时间的前1分钟之内是否有数据上传到数据库）
        // 当前时间的前1分钟在线的机床Id
        List<String> onlineDeviceGuids = accelerationMapper.selectOnlineDeviceGuids();

        List<MachineInfo> resultMachineInfos = new ArrayList<>();
        for (MachineInfo machineInfo: machineInfos){
            if (onlineDeviceGuids.contains(machineInfo.getDeviceGuid())){
                machineInfo.setOnline(1);
            }else {
                machineInfo.setOnline(0);
            }
            resultMachineInfos.add(machineInfo);
        }

        return resultMachineInfos;
    }
}
